import socket
import logging

data_tcp_port = 30000
data_udp_port = 30002


def start_tcp_server():
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_server_socket.bind(("0.0.0.0", data_tcp_port))
    while True:
        try:
            tcp_server_socket.listen(64)
            client_socket = tcp_server_socket.accept()
            recv_data = client_socket[0].recv(0x1000000)
            f = open("tcp_file.txt", "a+")
            f.write(recv_data.decode())
            f.close()
        except Exception as e:
            logging.error("DATA TCP Error:" + str(e))


def start_udp_server():
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    udp_socket.bind(('0.0.0.0', data_udp_port))
    while True:
        try:
            recv_data = udp_socket.recvfrom(0x1000000)
            f = open("udp_file.txt", "a+")
            f.write(recv_data.decode())
            f.close()
        except Exception as e:
            logging.error("DATA TCP Error:" + str(e))


if __name__ == '__main__':
    start_tcp_server()
